import getpass
import operator
import re
from com.mtc.util.connector import Connector


def get_next_page(page_data): 
  # [shang ye][<a href="next_page_url" class="news">xia ye</a>]
  return re.search(ur'\u00C9\u00CF\u00D2\u00B3.+\[<a href="(.+)" class="news">\u00CF\u00C2\u00D2\u00B3</a>\]', page_data)
  
  
def get_num_pages_for_members():
  url = 'http://www.mitbbs.com/club/mitbbs_club_member.php?club=Topcoders&pagen=1'
  page_data = bbs.get_page(url)
 
  # Grab the # total pages (text) beside the select drop-down box
  select_drop_down_pages = re.search(ur'</select>/(\d+)\u00D2\u00B3\]</form>', page_data)
  num_pages = select_drop_down_pages.group(1) if select_drop_down_pages else 0
  return int(num_pages)
 
 
def get_member_list():
  members = []
  num_pages = get_num_pages_for_members()
 
  for page in range(0, num_pages):
    url = 'http://www.mitbbs.com/club/mitbbs_club_member.php?club=Topcoders&pagen=%d' % (page + 1)
    page_data = bbs.get_page(url)
    members += re.findall(r'<TD align="center"><a href=javascript:myhref\("/user_info/[a-zA-Z0-9]+/[a-z0-9]+"\) class=news>([a-zA-Z0-9]+)</a></TD>', page_data)
 
  members.sort()
  return members


def get_authors_by_post_number(posts):
  sorted_post_by_count = sorted(posts.iteritems(), key = operator.itemgetter(1))
  sorted_post_by_count.reverse()
  return sorted_post_by_count



  
# Only put your password here for personal use
user = ''
passwd = ''

# Better to log in every time
if not user:
  user = raw_input('Please enter your user id: ')
if not passwd:
  passwd = getpass.getpass()


bbs = Connector(user, passwd)
posts = {}
# wen zhang mo shi (first page)
url = 'http://www.mitbbs.com/club_bbsdoc1/Topcoders_1_3.html'

while True:
  page_data = bbs.get_page(url)
  print "Processing page [" + url + "]..."
 
  # get all user names, user name can only contain alphanumeric characters, ie: [a-zA-Z0-9]
  users = re.findall(r'<a class="news1" href=\'/user_info/([a-zA-Z0-9]+)/[a-z0-9]+\'>\1</a></td>', page_data)
  for user in users:
    posts[user] = posts.get(user, 0) + 1
 
  next_page = get_next_page(page_data)
  if not next_page: 
    break 
    
  next_page_url = next_page.group(1)
  url = 'http://www.mitbbs.com' + next_page_url

  
print ""
print "Members who never posted" 
print "------------------------"

count = 0
for user in get_member_list() : 
  if posts.get(user, 0) == 0 :
    count = count + 1
    print user  
print "Total = ", count 
 
print ""
print "Members sorted by #posts"
print "------------------------"

i = 1 
for (user, num_posts) in get_authors_by_post_number(posts):
  print "%d. %s / %d" % (i, user, num_posts)
  i = i + 1 